package by.grsu.coctails.fragments;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import by.grsu.coctails.constants.ApplicationIntents;

public class CommonReceiverFragment extends Fragment {

	private BroadcastReceiver mReceiver;

	private boolean receiverRegistered = false;

	protected void registerReceiver() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(ApplicationIntents.ON_CHANDGE_INGREDIENT_INTENT);

		mReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				if (intent.getAction().equals(
						ApplicationIntents.ON_CHANDGE_INGREDIENT_INTENT)) {
					String name = intent
							.getStringExtra(ApplicationIntents.INGREDIENT_NAME_KEY);
					int state = intent.getIntExtra(
							ApplicationIntents.INGREDIENT_STATE_KEY, 0);
					onIngredientChandged(name, state);
				}

			}
		};

		getActivity().registerReceiver(mReceiver, filter);
		receiverRegistered = true;
	}

	protected void unRegisterReceiver() {
		if (mReceiver != null && receiverRegistered) {
			getActivity().unregisterReceiver(mReceiver);
			receiverRegistered = false;
		}
	}

	protected void onIngredientChandged(String name, int state) {

	}
}
